/*
Disclaimer of Warranty: THE PACKAGE IS PROVIDED
BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS'
AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT
ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF
THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


(c) Copyright 2013 Edward Principe
All Rights Reserved

*/
#include "parseall.h"

////////////////////////////////////////////////////////////////
// Copy: Copies class
////////////////////////////////////////////////////////////////
void ParseAll::Copy(const ParseAll& rhs)
{
    // Same object?
	if (this == &rhs)
		return;

    // Erase current words
	_words.erase(_words.begin(), _words.end());

    // Copy words
	for (list<ParseNode>::const_iterator i = rhs._words.begin(); i != rhs._words.end(); i++)
	{
		_words.push_back(static_cast<ParseNode>(*i));
	}

	// Set iterator to the beginning
	_w = _words.begin();
}

//////////////////////////////////////////////////////////////////
// InitBuffer: Initializes buffer
//////////////////////////////////////////////////////////////////
void ParseAll::InitBuffer(const wchar_t *buffer)
{
    // Erase current words
	_words.erase(_words.begin(), _words.end());

    // Set beginning node
	ParseNode pn1; // First parse node

	pn1.is_begin = true;

	_words.push_back(pn1);

    // Initilize buffer in back-end parser
	_parse->InitBuffer(buffer);

    // Parse the buffer
	ParseNode pn2; // Parse node for the middle

	while (true)
	{
        // At end?
		if (_parse->IsEnd() && _parse->GetWord() == L"")
			break;

        // Fill in parse node
		pn2.err = _parse->Next();
		pn2.word = _parse->GetWord();
		pn2.is_string = _parse->IsString();

        // Add parse node to the end of the words list
		_words.push_back(pn2);

        // At end?
		if (_parse->IsEnd())
			break;
	}

    // Add end node to list
	ParseNode pn3;

	pn3.is_end = true;

	_words.push_back(pn3);

	// Set iterator to the beginning
	_w = _words.begin();
}